package com.mygame.game_server;

import com.mygame.game_server.cmdHandle.CmdHandlerFactory;
import com.mygame.game_server.conf.AllConf;
import com.mygame.game_server.mq.MqProducer;
import com.mygame.game_server.msgcoder.GameMsgRecognizer;
import com.mygame.game_server.util.RedisUtil;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import org.apache.log4j.PropertyConfigurator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Jim
 * @date 19:42 2022/5/5
 * @description 启动类
 * @email:huwmz04153277@126.com
 */
public class ServerMain {
    private final static Logger logger = LoggerFactory.getLogger(ServerMain.class);
     public static void main(String[] args)  {
         PropertyConfigurator.configure(ServerMain.class.getClassLoader().getResourceAsStream("log4j.properties"));
         //初始化工厂
         CmdHandlerFactory.init();
         GameMsgRecognizer.init();
         MysqlSessionFactory.init();

         //初始化redis连接工具
         RedisUtil.init();

         MqProducer.init();




         EventLoopGroup boss = new NioEventLoopGroup(1);
         EventLoopGroup workerGroup = new NioEventLoopGroup();
         try {


             ServerBootstrap bootstrap = new ServerBootstrap();
             bootstrap.group(boss, workerGroup).channel(NioServerSocketChannel.class).
                     childHandler(new ChannelInitializer<SocketChannel>() {
                         @Override
                         protected void initChannel(SocketChannel socketChannel) throws Exception {
                             ChannelPipeline pipeline = socketChannel.pipeline();
                             pipeline.addLast(
                                     new HttpServerCodec(),
                                     new HttpObjectAggregator(65535),
                                     new WebSocketServerProtocolHandler("/websocket"),
                                     new GameMessageDecoder(),
                                     new GameMessageEncoder(),
                                     new GameMsgHandler());
                         }
                     });
             bootstrap.option(ChannelOption.SO_BACKLOG, 128); // 队列长度
             bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true); //开启心跳
             ChannelFuture future = bootstrap.bind(AllConf.GAME_SERVER_PORT).sync();
             if (future.isSuccess()) {
                 logger.info("------服务器启动成功------");
             }
             future.channel().closeFuture().sync();
         }catch (Exception e){
             logger.error(e.getMessage());
         }finally {
             workerGroup.shutdownGracefully();
             boss.shutdownGracefully();
         }
    }
}
